/**
 * @param cronExpression
 * @return '--' | 'error! 不合法的cron表达式' | string
 */
export function cronParserToCn(cronExpression?: string) {
  if (!cronExpression)
    return '--'
  const cronParts = cronExpression.split(' ')
  if (cronParts.length > 7 || cronParts.length < 5)
    return 'error! 不合法的cron表达式'

  const seconds = parseCronPart(cronParts[0], '秒钟')
  const minutes = parseCronPart(cronParts[1], '分钟')
  const hours = parseCronPart(cronParts[2], '小时')
  const dayOfMonth = parseCronPart(cronParts[3], '天')
  const month = parseCronPart(cronParts[4], '月')
  const dayOfWeek = parseCronPart(cronParts[5], '星期')
  const year = parseCronPart(cronParts[6], '年')

  let parsedExpression = ''

  if (year)
    parsedExpression += `在${year} `

  if (month)
    parsedExpression += `${month} `

  if (dayOfMonth && !dayOfWeek)
    parsedExpression += `${dayOfMonth} `

  if (dayOfMonth && dayOfWeek)
    parsedExpression += `${dayOfMonth} ${dayOfWeek} `

  if (!dayOfMonth && dayOfWeek)
    parsedExpression += `${dayOfWeek} `

  if (hours)
    parsedExpression += `${hours} `

  if (minutes)
    parsedExpression += `${minutes} `

  if (seconds)
    parsedExpression += `${seconds} `

  parsedExpression += '触发'

  return parsedExpression.trim()
}

function parseCronPart(cronPart: string, partName: string) {
  if (!cronPart)
    return ''
  let parsedPart = ''
  if (cronPart === '*') {
    parsedPart = `每${partName}里`
  }
  else if (cronPart.includes('-')) {
    const [start, end] = cronPart.split('-')
    if (partName == '星期')
      parsedPart = `从${partName}${start}到${partName}${end}里`
    else
      parsedPart = `从${start}到${end}的每${partName}里`
  }
  else if (cronPart.includes('/')) {
    const [value, interval] = cronPart.split('/')
    if (partName == '星期')
      parsedPart = `从${partName}${value}开始的每隔${interval}天里`
    else
      parsedPart = `从第${value}${partName}开始的每隔${interval}${partName}里`
  }
  else if (cronPart.includes(',')) {
    const values = cronPart.split(',')
    parsedPart = ''
    values.forEach((value, index) => {
      if (partName == '星期')
        parsedPart += `${partName}${value}的时候`
      else
        parsedPart += `第${value}${partName}的时候`

      if (index !== values.length - 1)
        parsedPart += '和'
    })
  }
  else if (cronPart.startsWith('L')) {
    parsedPart = `最后一个${partName}里`
  }
  else if (cronPart.endsWith('L')) {
    const value = cronPart.slice(0, -1)
    if (partName == '星期')
      parsedPart = `当月最后一个${partName}${value}里`
    else
      parsedPart = `${value}号的最后一个${partName}里`
  }
  else if (cronPart.includes('#')) {
    const [value, weekday] = cronPart.split('#')
    parsedPart = `第${value}周的${partName}${weekday}里`
  }
  else if (cronPart.includes('W')) {
    const value = cronPart.slice(0, -1)
    parsedPart = `最接近${value}号的工作日里`
  }
  else if (cronPart === '?') {
    parsedPart = ''
  }
  else {
    if (partName == '星期')
      parsedPart = `${partName}${cronPart}里`
    else
      parsedPart = `第${cronPart}${partName}里`
  }
  return parsedPart
}

// 测试方式
/*
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function parseCronExpression(cronExpression) {
      if (!cronExpression) return '--'
      const cronParts = cronExpression.split(' ')
      if (cronParts.length > 7 || cronParts.length < 5) {
        return 'error! 不合法的cron表达式'
      }
      const seconds = parseCronPart(cronParts[0], '秒钟');
      const minutes = parseCronPart(cronParts[1], '分钟');
      const hours = parseCronPart(cronParts[2], '小时');
      const dayOfMonth = parseCronPart(cronParts[3], '天');
      const month = parseCronPart(cronParts[4], '月');
      const dayOfWeek = parseCronPart(cronParts[5], '星期');
      const year = parseCronPart(cronParts[6], '年');

      let parsedExpression = '';

      if (year) {
        parsedExpression += `在${year} `;
      }

      if (month) {
        parsedExpression += `${month} `;
      }

      if (dayOfMonth && !dayOfWeek) {
        parsedExpression += `${dayOfMonth} `;
      }

      if (dayOfMonth && dayOfWeek) {
        parsedExpression += `${dayOfMonth} ${dayOfWeek} `;
      }

      if (!dayOfMonth && dayOfWeek) {
        parsedExpression += `${dayOfWeek} `;
      }

      if (hours) {
        parsedExpression += `${hours} `;
      }

      if (minutes) {
        parsedExpression += `${minutes} `;
      }

      if (seconds) {
        parsedExpression += `${seconds} `;
      }

      parsedExpression += '触发';

      return parsedExpression.trim();
    }

    function parseCronPart(cronPart, partName) {
      let parsedPart = '';

      if (cronPart === '*') {
        parsedPart = `每${partName}里`;
      } else if (cronPart.includes('-')) {
        const [start, end] = cronPart.split('-');
        if (partName == '星期') {
          parsedPart = `从${partName}${start}到${partName}${end}里`;
        } else {
          parsedPart = `从${start}到${end}的每${partName}里`;
        }
      } else if (cronPart.includes('/')) {
        const [value, interval] = cronPart.split('/');
        if (partName == '星期') {
          parsedPart = `从${partName}${value}开始的每隔${interval}天里`;
        } else {
          parsedPart = `从第${value}${partName}开始的每隔${interval}${partName}里`;
        }
      } else if (cronPart.includes(',')) {
        const values = cronPart.split(',');
        parsedPart = '';
        values.forEach((value, index) => {
          if (partName == '星期') {
            parsedPart += `${partName}${value}的时候`;
          } else {
            parsedPart += `第${value}${partName}的时候`;
          }
          if (index !== values.length - 1) {
            parsedPart += '和';
          }
        });
      } else if (cronPart.startsWith('L')) {
        parsedPart = `最后一个${partName}里`;
      } else if (cronPart.endsWith('L')) {
        const value = cronPart.slice(0, -1);
        if (partName == '星期') {
          parsedPart = `当月最后一个${partName}${value}里`;
        } else {
          parsedPart = `${value}号的最后一个${partName}里`;
        }
      } else if (cronPart.includes('#')) {
        const [value, weekday] = cronPart.split('#');
        parsedPart = `第${value}周的${partName}${weekday}里`;
      } else if (cronPart.includes('W')) {
        const value = cronPart.slice(0, -1);
        parsedPart = `最接近${value}号的工作日里`;
      } else if (cronPart === '?') {
        parsedPart = '';
      } else {
        if (partName == '星期') {
          parsedPart = `${partName}${cronPart}里`;
        } else {
          parsedPart = `第${cronPart}${partName}里`;
        }
      }

      return parsedPart;
    }

    // test
    const divElement = document.createElement('div');
    divElement.innerHTML =
      '秒钟的变化' + '<br>' +
      parseCronExpression('* * * * * ? *') + '<br>' +
      parseCronExpression('1-2 * *  * ? *') + '<br>' +
      parseCronExpression('0/1 * * * * ? *') + '<br>' +
      parseCronExpression('10 * * * * ? *') + '<br>' +
      parseCronExpression('10,20,30 * * * * ? *') + '<br>' +

      '分钟的变化' + '<br>' +
      parseCronExpression('* * * * * ? *') + '<br>' +
      parseCronExpression('* 1-2 *  * ? *') + '<br>' +
      parseCronExpression('* 0/1 * * * ? *') + '<br>' +
      parseCronExpression('* 10 * * * ? *') + '<br>' +
      parseCronExpression('* 10,20,30 * * * ? *') + '<br>' +

      '小时的变化' + '<br>' +
      parseCronExpression('* * * * * ? *') + '<br>' +
      parseCronExpression('* * 1-2 * * ? *') + '<br>' +
      parseCronExpression('* * 0/1 * * ? *') + '<br>' +
      parseCronExpression('* * 10 * * ? *') + '<br>' +
      parseCronExpression('* * 10,20,30 * * ? *') + '<br>' +

      '天的变化' + '<br>' +
      parseCronExpression('* * * L * ? *') + '<br>' +
      parseCronExpression('* * * 1W * ? *') + '<br>' +
      parseCronExpression('* * * 3 * ? *') + '<br>' +
      parseCronExpression('* * * 3,10 * ? *') + '<br>' +
      parseCronExpression('* * * * * ? *') + '<br>' +
      parseCronExpression('* * * 1-2 * ? *') + '<br>' +
      parseCronExpression('* * * 1/2 * ? *') + '<br>' +

      '月的变化' + '<br>' +
      parseCronExpression('* * * * * ? *') + '<br>' +
      parseCronExpression('* * * * 1-2 ? *') + '<br>' +
      parseCronExpression('* * * * 1/1 ? *') + '<br>' +
      parseCronExpression('* * * * 10 ? *') + '<br>' +
      parseCronExpression('* * * * 1,10 ? *') + '<br>' +

      '星期的变化' + '<br>' +
      parseCronExpression('* * * ? * * *') + '<br>' +
      parseCronExpression('* * * ? * 1/2 *') + '<br>' +
      parseCronExpression('* * * ? * 1-5 *') + '<br>' +
      parseCronExpression('* * * ? * 2#1 *') + '<br>' +
      parseCronExpression('* * * ? * 2L *') + '<br>' +
      parseCronExpression('* * * ? * 1 *') + '<br>' +
      parseCronExpression('* * * ? * 1,2,3 *') + '<br>' +

      '年的变化' + '<br>' +
      parseCronExpression('* * * * * ? *') + '<br>' +
      parseCronExpression('* * * * * ? 2024') + '<br>' +
      parseCronExpression('* * * * * ? 2024,2025') + '<br>' +
      parseCronExpression('* * * * * ? 2024/1') + '<br>' +
      parseCronExpression('* * * * * ? 2024-2030') + '<br>' +

      ''
    document.body.append(divElement)
  </script>
</body>

</html>
*/
